<?php
namespace app\bz\controller;
use think\Db;
class Goods extends Base
{
    private $tn = 'goods';

    public function index()
    {
        if (isset($this->get['page']) && isset($this->get['limit'])) {
            $where = [];

            if (isset($this->get['goods'])) {
                $where['title'] = ['like',"%". $this->get['goods']."%"];
            }  
  
            $list  = db($this->tn)->field(true)->where($where)->order('id', 'desc')->page($this->get['page'])->limit($this->get['limit'])->select();
            foreach ($list as $k => $v) {
            }
            $count = db($this->tn)->where($where)->count();
            $this->returnAPI('', 0, $list, ['count' => $count, 'limit' => $this->get['limit']]);
        }
        return $this->fetch();
    }

    public function edit($id = 0)
    {
        $model = model($this->tn);
        if ($this->post) {
            
            $this->post['banner'] = implode(',',$this->post['banner']);
            if ($id) {
                $model = $model->get($id);
                if (is_null($model)) {
                    $this->returnAPI('信息有误,请稍后重试');
                }
                if(isset($this->post['type'])&& $this->post['type']!=''){
                    db('goods_type')->where('goods_id',$id)->delete();
                    foreach ($this->post['type'] as $key => $value) {
                        db('goods_type')->insert(['goods_id'=>$id,'type'=>$value]);
                    }
                }
                if(isset($this->post['lable'])&& $this->post['lable']!=''){
                    db('goods_lable')->where('goods_id',$id)->delete();
                    foreach ($this->post['lable'] as $key => $value) {
                        db('goods_lable')->insert(['goods_id'=>$id,'lable'=>$value]);
                    }
                }
                $result = $model->validate($this->tn)->allowField(true)->save($this->post);
            }else{
                $result = $model->insertGetId(['title'=>$this->post['title'],'price'=>$this->post['price'],'banner'=>$this->post['banner'],'sort'=>$this->post['sort'],'surplus_num'=>$this->post['surplus_num'],'content'=>$this->post['content']]);
                if(isset($this->post['type'])&& $this->post['type']!=''){
                    //db('goods_type')->where('goods_id',$id)->delete();
                    foreach ($this->post['type'] as $key => $value) {
                        db('goods_type')->insert(['goods_id'=>$result,'type'=>$value]);
                    }
                }

                if(isset($this->post['lable'])&& $this->post['lable']!=''){
                    //db('goods_type')->where('goods_id',$id)->delete();
                    foreach ($this->post['lable'] as $key => $value) {
                        db('goods_lable')->insert(['goods_id'=>$result,'lable'=>$value]);
                    }
                }
            }
            
            if ($result !== false) {
                $this->returnAPI('操作成功', 0);
            }
            $this->returnAPI($model->getError());
        }
        $model = $model->get($id);
        if (is_null($model)) {
            $data = $this->table_fields($this->tn);
        } else {
            $data = $model->getData();
            if($data['banner'])
                $data['banner'] = explode(',',$data['banner']); 
            $data['type'] = db('goods_type')->where('goods_id',$data['id'])->select();
            $data['lable'] = db('goods_lable')->where('goods_id',$data['id'])->select();
        }
        $type = db('type_goods')->select();
        return $this->fetch('', ['data' => $data,'type'=>$type]);
    }

        //上传图片
    public function upload(){
        //echo"<pre>";print_r();die;
       // echo"<pre>";print_r(request()->file('file'));die;
        //testUploadLocalImage('LTAI0rLnWqXA0Jc2','oQEjYeWHPuZgzdJvclnOkzRd43yqJ9', $_FILES['file']['tmp_name']);
        $files = request()->file('file');
        $path = 'banner';
        $base_path = './upload/';
        $upload_path = $path == '' ? $base_path : $base_path.$path.'/';
        @mkdir($upload_path,0777);
        $image = \think\Image::open($files);
        
        if(!in_array($image->type(),['jpg','jpeg','png'])){

            $this->returnAPI('图片格式错误', 0);
        }
        @mkdir($upload_path,0777);
        $name = md5(date('YmdHis').rand(1000,9999).rand(1000,9999)).'.'.$image->type();

        $info = $files->move($upload_path);
        $this->returnAPI('操作成功', 1,substr($upload_path,1).date('Ymd').'/'.$info->getFilename());
    }

//删除图片
    public function del_image($file){
        if(file_exists('.'.$file)){
            unlink('.'.$file);
        }
        $this->returnAPI('删除成功',1);
    }

    //删除类型
    public function removetype(){
        $query = db('goods_type')->where('id',$this->post['id'])->delete();
        if(!$query)
           $this->returnAPI('删除失败'); 
        $this->returnAPI('删除成功',1);
    }
}
